<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>新增采集信息</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="../lib/layui/css/layui.css" media="all">
    <style type="text/css">
        .redstar {
            color: red;
            margin-right: 5px;
        }

        .layui-field-title {
            color: #ffffff;
        }

    </style>
</head>
<body>
<div class="layuimini-container">
    <div class="layuimini-main">
        <form class="layui-form" lay-filter="collectInfo" action="" style="margin:10px 20px 0 10px">
            <input type="text" id="id" name="id" class="layui-input" style="display: none">
            <input type="text" id="tableId" name="tableId" class="layui-input" style="display: none">
            <div class="layui-form-item">
                <label class="layui-form-label">
                    <i class="redstar">*</i>年度</label>
                <div class="layui-input-block">
                    <input class="layui-input" id="year" name="year" lay-verify="year" autocomplete="off" placeholder="请输入年度">
<!--                    <select class="layui-select" id="year" name="year" lay-filter="collection">-->
<!--                        <option value="">请选择年度</option>-->
<!--                    </select>-->
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">
                    <i class="redstar">*</i>分类</label>
                <div class="layui-input-block">
                    <select class="layui-select" id="datasourceId" lay-verify="required" name="datasourceId"
                            lay-filter="collection" lay-search>
                        <option value="">请选择分类</option>
                    </select>
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">
                    <i class="redstar">*</i>转换任务</label>
                <div class="layui-input-block">
                    <select class="layui-select" id="fromTable" lay-verify="required" name="fromTable"
                            lay-filter="transcollection" lay-search>
                        <option value="">请选择转换任务</option>
                    </select>
                    <!--<input class="layui-input" id="fromTable" name="fromTable" lay-verify="required" autocomplete="off"-->
                           <!--placeholder="请输入源表,表之间用逗号隔开">-->
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">
                    <i class="redstar">*</i>采集规则描述</label>
                <div class="layui-input-block">
                    <textarea class="layui-input" id="description" name="description" lay-verify="required" autocomplete="off" style="height: 125px"></textarea>
                </div>
            </div>
            <div class="layui-form-item layui-hide">
                <div class="layui-input-block">
                    <button id="yes" class="layui-btn yes" lay-submit lay-filter="submit" style="display: none">确定
                    </button>
                </div>
            </div>
            <input type="hidden" class="layui-input" value="" name="datasourceName" id="datasourceName">
            <input type="hidden" class="layui-input" value="" name="fromTableName" id="fromTableName">
        </form>
    </div>
</div>


<script src="../lib/layui/layui.js" charset="utf-8"></script>
<script src="../js/jquery-1.9.0.js"></script>
<script type="text/javascript" src="../js/common.js"></script>
<script src="../js/common/request.js"></script>

<script>
    layui.use(['form', 'element', 'table'], function () {
        var element = layui.element,
            form = layui.form,
            table = layui.table;

        /**
         * 获取初始数据
         */
        var id = getUrlParams('id'),
            tableId = getUrlParams('tableId'),
            year = getUrlParams('year'),
            datasourceId = getUrlParams('datasourceId'),
            fromTable = getUrlParams('fromTable'),
            description = getUrlParams('description'),
            datasourceName = getUrlParams('datasourceName'),
            fromTableName = getUrlParams('fromTableName');

        $(document).ready(function () {
            getDatasource();
            getFromTable();
            if (id !== null && id !== undefined) {
                form.val('collectInfo', {
                    'id':id,
                    'tableId': tableId,
                    'datasourceId': datasourceId,
                    'fromTable': fromTable,
                    'year': year,
                    'description': description,
                    'datasourceName': datasourceName,
                    'fromTableName': fromTableName
                });
            } else {
                form.val('collectInfo', {
                    'tableId': tableId
                });
            }
        });

        /**
         * 配置约束
         */
        form.verify({
            year:function (value,item){
                if(isNaN(value) || (parseInt(value)>2999 || parseInt(value)<1999)) {
                // if(isNaN(value)) {
                    return '请输入规范年度';
                }
            }
            // number1: function (value, item) {
            //     if (!isNaN(value)) {
            //         return '小数只可输入数字';
            //     }
            // }
        });


        /**
         * 监听提交按钮点击
         */
        window.addEventListener('message', function (event) {
            if (event.data === 'submit') {
                $("#yes").trigger("click");
            }
        });


        /**
         * 监听提交
         */
        form.on('submit(submit)', function (data) {
            if (data.field.id != null && data.field.id !== '') {
                /**
                 * 修改采集信息
                 */
                $.ajax({
                    type: "post",
                    url: "/baseDataManagerServer/modelCollectInfo/updateModelCollectInfo",
                    dataType: 'json',
                    cache: false,
                    contentType: "application/json",
                    data: JSON.stringify(data.field),
                    success: function (redata) {
                        if (redata.code === '0000') {
                            window.parent.postMessage('update_success-' + redata.msg, '*');
                        } else if (redata.code === '9999') {
                            window.parent.postMessage('update_error-' + redata.msg, '*');
                        }
                    },
                    error: function (redata) {
                        window.parent.postMessage('update_error', '*');
                    }
                });
            } else {
                $.ajax({
                    type: "post",
                    url: "/baseDataManagerServer/modelCollectInfo/insertModelCollectInfo",
                    dataType: 'json',
                    cache: false,
                    contentType: "application/json",
                    data: JSON.stringify(data.field),
                    success: function (redata) {
                        if (redata.code === '0000') {
                            window.parent.postMessage('success-' + redata.msg, '*');
                        } else if (redata.code === '9999') {
                            window.parent.postMessage('error-' + redata.msg, '*');
                        }
                    },
                    error: function (redata) {
                        window.parent.postMessage('error', '*');
                    }
                });
            }
            return false;
        });

        //监听分类选择
        form.on('select(collection)', function(data){
            $("#datasourceName").val(data.elem[data.elem.selectedIndex].text)
            var str = '<option value="">请选择转换任务</option>';
            var data =   {"page": {
                "number": 1,
                    "size": 1100
            },"query": {
                    "categoryId": data.value
                }}
            //data.value 得到被选中的值
            jQuery.support.cors = true;
            $.ajax({
                type: "POST",
                url: '/dataCollection/trans/monitor/findTransMonitorListByPage.do',
                dataType:"json",
                timeout : 5000,
                cache:false,
                contentType:"application/json",
                data: JSON.stringify(data),
                async: true,//使用同步方式，目前data组件有同步依赖
                success: function(data){
                    console.log(data)
                    data.result.content.forEach(item=>{
                        str+='<option value="'+item.monitorTransId+'">'+item.transName+'</option>';
                    });
                    $('#fromTable').html(str);
                    layui.form.render("select");
                },
                error: function(XMLHttpRequest, textStatus, errorThrown){
                    console.log(data)
                },
                complete:function(XMLHttpRequest,status){
                }

            });
            // $.get(url,function(data){
            //     $("#two_cate").empty();
            //     $("#two_cate").append(new Option("请选择分类",""));
            //     $.each(data,function(index,item){
            //         $("#two_cate").append(new Option(item,index));
            //         console.log(index,item);
            //     });
            //     layui.form.render("select");
            // });

        });

        //监听转换任务选择
        form.on('select(transcollection)', function(data){
            $("#fromTableName").val(data.elem[data.elem.selectedIndex].text)
        });

        /**
         * 加载采集来源
         */
        function getDatasource(){
            var str = '<option value="">请选择分类</option>';
            // var datasourceInfo = request.get('/strategy/queryDataSource');
            // datasourceInfo.data.forEach(item=>{
            //     str+='<option value="'+item.id+'">'+item.name+'</option>';
            // });
            //现在改成获取分类接口
            var datasourceInfo = request.get('/dataCollection/category/findCategoryList.do');
            datasourceInfo.result.forEach(item=>{
                str+='<option value="'+item.id+'">'+item.categoryName+'</option>';
            });
            $('#datasourceId').html(str);
        }

        function getFromTable() {
            if (id === null || id === undefined) {
                return
            }
            var str = '<option value="">请选择转换任务</option>';
            var data =   {"page": {
                    "number": 1,
                    "size": 1100
                },"query": {
                    "categoryId": datasourceId
                }}
            //data.value 得到被选中的值
            jQuery.support.cors = true;
            $.ajax({
                type: "POST",
                url: '/dataCollection/trans/monitor/findTransMonitorListByPage.do',
                dataType:"json",
                timeout : 5000,
                cache:false,
                contentType:"application/json",
                data: JSON.stringify(data),
                async: true,//使用同步方式，目前data组件有同步依赖
                success: function(data){
                    data.result.content.forEach(item=>{
                        str+='<option value="'+item.monitorTransId+'">'+item.transName+'</option>';
                });
                    $('#fromTable').html(str);
                    $("#fromTable option[value='"+fromTable+"']").attr("selected","selected"); //强制绑定
                    layui.form.render("select");
                },
                error: function(XMLHttpRequest, textStatus, errorThrown){
                    console.log(data)
                },
                complete:function(XMLHttpRequest,status){
                }

            });
        }
    });


    window.getUrlParams = function (key) {
        //获取url参数
        var reg = new RegExp("(^|&)" + key + "=([^&]*)(&|$)", "i");
        var url = decodeURI(window.location.search)
        var r = url.substr(1).match(reg);
        if (r != null) return unescape(r[2]);
        return null;
    }
</script>

</body>
</html>